//
//  AnswerViewController.m
//  CulinaryKnowledge
//
//  Created by CaoHung on 7/20/14.
//  Copyright (c) 2014 GreenSun. All rights reserved.
//

#import "AnswerViewController.h"
#import "FinishViewController.h"

#define isiPhone5  ([[UIScreen mainScreen] bounds].size.height == 568)?TRUE:FALSE
#define IS_OS_7    ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)

@interface AnswerViewController ()

@end

@implementation AnswerViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"うなぎ検定";
    if (IS_OS_7) {
        UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"backButton.png"] style:UIBarButtonItemStylePlain target:self action:@selector(backToHome)];
        self.navigationItem.leftBarButtonItem = backButton;
    }
    else{
        UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"タイトル" style:UIBarButtonItemStylePlain target:self action:@selector(backToHome)];
        self.navigationItem.leftBarButtonItem = backButton;
    }
    if (self.mIndex >= 4) {
        UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
        aButton.frame = CGRectMake(10, 623, 279, 51);
        [aButton addTarget:self action:@selector(goToshogakukan) forControlEvents:UIControlEventTouchUpInside];
        [self.myScrollView addSubview:aButton];
        
        UIButton *amazonButton = [UIButton buttonWithType:UIButtonTypeCustom];
        amazonButton.frame = CGRectMake(10, 690, 279, 51);
        [amazonButton addTarget:self action:@selector(goToAmazon) forControlEvents:UIControlEventTouchUpInside];
        [self.myScrollView addSubview:amazonButton];
    }

    //Layout screen
    [self layoutScreen];
}

// Go to shogakukan shop
- (void)goToshogakukan
{
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.shogakukan.co.jp/books/detail/_isbn_9784093883511"]];
}

// Go to Amazon
- (void)goToAmazon
{
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.amazon.co.jp/dp/4093883513"]];
}

// Layout screen
- (void)layoutScreen
{
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background_Detail4inch.png"]];
    self.tietleAnswerView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:[NSString stringWithFormat:@"Image_title_question%ld.png", (long)self.mIndex]]];
    
    UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"contentAnswer%ld", (long)self.mIndex]];
    self.contentAnswerImageView.image = image;
    
    if (self.mIndex == 1) {
        [self.btnNext setImage:[UIImage imageNamed:@"next_ButtonImage.png"] forState:UIControlStateNormal];
        self.myScrollView.contentSize = CGSizeMake(291.0f, 1660.0f);
        CGRect frame = CGRectMake(19.0f, 0.0f, 256.0f, 1654.0f);
        self.contentAnswerImageView.frame = frame;
    }
    else if(self.mIndex == 2){
        [self.btnNext setImage:[UIImage imageNamed:@"next_ButtonImage.png"] forState:UIControlStateNormal];
        self.myScrollView.contentSize = CGSizeMake(291.0f, 830.0f);
        CGRect frame = CGRectMake(19.0f, 0.0f, 256.0f, 824.0f);
        self.contentAnswerImageView.frame = frame;

    }
    else if(self.mIndex == 3){
        [self.btnNext setImage:[UIImage imageNamed:@"next_ButtonImage.png"] forState:UIControlStateNormal];
        self.myScrollView.contentSize = CGSizeMake(291.0f, 1700.0f);
        CGRect frame = CGRectMake(19.0f, 0.0f, 256.0f, 1700.0f);
        self.contentAnswerImageView.frame = frame;
    }
    else if(self.mIndex == 4){
        [self.btnNext setImage:[UIImage imageNamed:@"next_ButtonImage.png"] forState:UIControlStateNormal];
        self.myScrollView.contentSize = CGSizeMake(291.0f, 1112.0f);
        CGRect frame = CGRectMake(7.0f, 0.0f, 285.0f, 1089.0f);
        self.contentAnswerImageView.frame = frame;
    }
    else if(self.mIndex == 5){
        [self.btnNext setImage:[UIImage imageNamed:@"next_ButtonImage.png"] forState:UIControlStateNormal];
        self.myScrollView.contentSize = CGSizeMake(291.0f, 1112.0f);
        CGRect frame = CGRectMake(19.0f, 0.0f, 256.0f, 403.0f);
        self.contentAnswerImageView.frame = frame;
        
        // Add detail answer
        CGRect frameImage = self.contentAnswerImageView.frame;
        UIImageView *detailImage = [[UIImageView alloc]initWithFrame:CGRectMake(7.0f , frameImage.origin.y + frameImage.size.height, frameImage.size.width + 30.0f, 688.0f)];
        [detailImage setImage:[UIImage imageNamed:@"detailAnswerImage.png"]];
        
        [self.myScrollView addSubview:detailImage];
    }
    else if(self.mIndex == 6){
        [self.btnNext setImage:[UIImage imageNamed:@"next_ButtonImage.png"] forState:UIControlStateNormal];
        self.myScrollView.contentSize = CGSizeMake(291.0f, 1112.0f);
        CGRect frame = CGRectMake(19.0f, 0.0f, 256.0f, 403.0f);
        self.contentAnswerImageView.frame = frame;
        
        // Add detail answer
        CGRect frameImage = self.contentAnswerImageView.frame;
        UIImageView *detailImage = [[UIImageView alloc]initWithFrame:CGRectMake(7.0f , frameImage.origin.y + frameImage.size.height, frameImage.size.width + 30.0f, 688.0f)];
        [detailImage setImage:[UIImage imageNamed:@"detailAnswerImage.png"]];
        
        [self.myScrollView addSubview:detailImage];
    }
    else if(self.mIndex == 7){
        [self.btnNext setImage:[UIImage imageNamed:@"next_ButtonImage.png"] forState:UIControlStateNormal];
        self.myScrollView.contentSize = CGSizeMake(291.0f, 1112.0f);
        CGRect frame = CGRectMake(19.0f, 0.0f, 256.0f, 403.0f);
        self.contentAnswerImageView.frame = frame;
        
        // Add detail answer
        CGRect frameImage = self.contentAnswerImageView.frame;
        UIImageView *detailImage = [[UIImageView alloc]initWithFrame:CGRectMake(7.0f , frameImage.origin.y + frameImage.size.height, frameImage.size.width + 30.0f, 688.0f)];
        [detailImage setImage:[UIImage imageNamed:@"detailAnswerImage.png"]];
        
        [self.myScrollView addSubview:detailImage];
    }
    else if(self.mIndex == 8){
        [self.btnNext setImage:[UIImage imageNamed:@"next_ButtonImage.png"] forState:UIControlStateNormal];
        self.myScrollView.contentSize = CGSizeMake(291.0f, 1112.0f);
        CGRect frame = CGRectMake(19.0f, 0.0f, 256.0f, 403.0f);
        self.contentAnswerImageView.frame = frame;
        
        // Add detail answer
        CGRect frameImage = self.contentAnswerImageView.frame;
        UIImageView *detailImage = [[UIImageView alloc]initWithFrame:CGRectMake(7.0f , frameImage.origin.y + frameImage.size.height, frameImage.size.width + 30.0f, 688.0f)];
        [detailImage setImage:[UIImage imageNamed:@"detailAnswerImage.png"]];
        
        [self.myScrollView addSubview:detailImage];
    }
    else if(self.mIndex == 9){
        [self.btnNext setImage:[UIImage imageNamed:@"Button_End.png"] forState:UIControlStateNormal];
        self.myScrollView.contentSize = CGSizeMake(291.0f, 1112.0f);
        CGRect frame = CGRectMake(19.0f, 0.0f, 256.0f, 403.0f);
        self.contentAnswerImageView.frame = frame;
        
        // Add detail answer
        CGRect frameImage = self.contentAnswerImageView.frame;
        UIImageView *detailImage = [[UIImageView alloc]initWithFrame:CGRectMake(7.0f , frameImage.origin.y + frameImage.size.height, frameImage.size.width + 30.0f, 688.0f)];
        [detailImage setImage:[UIImage imageNamed:@"detailAnswerImage.png"]];
        
        [self.myScrollView addSubview:detailImage];
    }
    else if(self.mIndex == 10){
        [self.btnNext setImage:[UIImage imageNamed:@"Button_Finish.png"] forState:UIControlStateNormal];
        self.myScrollView.contentSize = CGSizeMake(291.0f, 1112.0f);
        CGRect frame = CGRectMake(19.0f, 0.0f, 256.0f, 403.0f);
        self.contentAnswerImageView.frame = frame;
        
        // Add detail answer
        CGRect frameImage = self.contentAnswerImageView.frame;
        UIImageView *detailImage = [[UIImageView alloc]initWithFrame:CGRectMake(7.0f , frameImage.origin.y + frameImage.size.height, frameImage.size.width + 30.0f, 688.0f)];
        [detailImage setImage:[UIImage imageNamed:@"detailAnswerImage.png"]];
        
        [self.myScrollView addSubview:detailImage];
    }
    
    if (isiPhone5) {
        if(IS_OS_7){
            self.backgroundAnswer.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background_Answer4inch"]];
            self.backgroundAnswer.frame = CGRectMake(15.0f, 101.0f, 291.0f, 410.0f);
        }
        else{
            self.backgroundAnswer.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background_Answer4inch"]];
            self.backgroundAnswer.frame = CGRectMake(15.0f, 40.0f, 291.0f, 410.0f);
            self.tietleAnswerView.frame = CGRectMake(15.0f, 10.0f, 291.0f, 30.0f);
            
            CGRect frameButton = self.btnNext.frame;
            frameButton.origin.y = frameButton.origin.y - 65.0f;
            self.btnNext.frame = frameButton;
        }
    }
    else{
        
        // Set content frame
        CGRect contentFrame = self.contentAnswerImageView.frame;
        contentFrame.size.height = contentFrame.size.height + 70.0f;
        self.contentAnswerImageView.frame = contentFrame;
        
        CGRect frame = self.myScrollView.frame;
        self.backgroundAnswer.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background_Answer3.5inch.png"]];
        if (IS_OS_7) {
            self.myScrollView.frame = CGRectMake(frame.origin.x, frame.origin.y + 68.0f, frame.size.width, frame.size.height - 67.0f);
            self.backgroundAnswer.frame = CGRectMake(15.0f, 101.0f, 291.0f, 342.0f);
            
            CGRect frameButton = self.btnNext.frame;
            frameButton.origin.y = frameButton.origin.y - 80.0f;
            self.btnNext.frame = frameButton;
        }
        else{
            self.myScrollView.frame = CGRectMake(frame.origin.x, frame.origin.y + 68.0f, frame.size.width, frame.size.height - 67.0f);
            self.backgroundAnswer.frame = CGRectMake(15.0f, 40.0f, 291.0f, 342.0f);
            self.tietleAnswerView.frame = CGRectMake(15.0f, 10.0f, 291.0f, 30.0f);

            CGRect frameButton = self.btnNext.frame;
            frameButton.origin.y = frameButton.origin.y - 142.0f;
            self.btnNext.frame = frameButton;
        }
    }
}

// Go to next question
- (void)actionNext
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"nextQuestion" object:self];
    
    CATransition* transition = [CATransition animation];
    transition.duration = 0.30;
    transition.type = kCATransitionPush;
    transition.subtype = kCATransitionFromRight;
    
    [self.navigationController.view.layer addAnimation:transition forKey:@"animateOpacity"];
    [self.navigationController popViewControllerAnimated:NO];
    [CATransaction commit];
}

- (IBAction)actionNext:(id)sender {
    if (self.mIndex < 10) {
        [self actionNext];
    }else{
        [self actionFinish];
    }
}


// Go to finish screen
- (void)actionFinish
{
    FinishViewController *viewContronller = [[FinishViewController alloc]init];
    [viewContronller setMPoints:self.mPoints];
    
    [self.navigationController pushViewController:viewContronller animated:YES];
}

// Back to Home screen
- (void)backToHome
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

-(void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
